#!/usr/bin/python
 
## get system info
## get disk use stats
## get process stats
## get mem && swap use
 
import os
import platform
import commands
import pymongo
 
mongoHost = '127.0.0.1'
mongoPort = 27017
 
def mongod(data):
    conn = pymongo.Connection(mongoHost,mongoPort)
    db = conn.info
    db.hosts.remove({'_id':data['_id']})
    db.hosts.insert(data)
    conn.close()
 
class ServerStatus():
    def __init__(self):
        ## ('Windows', 'jackPeng-PC', '7', '6.1.7601', 'AMD64', 'Intel64 Family 6 Model 37
        ##   Stepping 5, GenuineIntel')
        ## ('Linux', 'localhost.localdomain', '2.6.32-279.el6.x86_64', '#1 SMP Fri Jun 22 12:19:21 UTC 2012', 'x86        ##   _64', 'x86_64')
        self.Plat = platform.uname()
        self.ipaddr = self.getIpaddr()
        self.System = self.getSystemStatus()
        self.Disk = self.getDiskStatus()
        self.Mem = self.getMemStatus()
        self.StatsList = {'_id':self.ipaddr,'system':self.System,'disk':self.Disk,'mem':self.Mem}           
 
 
    def R(self):
 
        return self.StatsList
 
    def getIpaddr(self):
        ipaddr = commands.getoutput("""ip addr list |grep "inet " | sed '/127.0.0/d' | head -1 |awk '{print $2}' |cut -d/ -f1""")
        return ipaddr 
 
    def getSystemStatus(self):
        ## if self.Plat[0] != 'Windows':
            ## get process number
        procNumber = commands.getoutput('ls /proc | wc -l')
        runTime = commands.getoutput('cat /proc/uptime').split(' ') 
        loadSys = commands.getoutput("w | head -1 |sed 's/,//g'|awk '{print $8,$9,$10}'")
         
        sysStat = {'sysproc':procNumber,'sysuptime':runTime[0],'sysFreetime':runTime[1],'sysLoad':loadSys}
 
        return sysStat
 
         
    def getMemStatus(self):
        ##  total used  free
        ##  336   186   150
        ##  991   0     991
        memString = commands.getoutput("free -m |awk '{if ( $2 ~ /[0-9]+/) print $2,$3,$4}'").split('\n')
        memStat = memString[0].split(' ')
        swapStat = memString[1].split(' ')
        memStatus = {'memtotal':memStat[0],'memused':memStat[1],'memfree':memStat[2],\
                    'swaptotal':swapStat[0],'swapused':swapStat[1],'swapfree':swapStat[2]}
 
        return memStatus
         
    def getDiskStatus(self):
        ## Filesystem    Type   1024-blocks      Used Available Capacity Mounted on
        ## /dev/mapper/VolGroup-lv_root ext4   8813300   2762512   5603096      34% /
        ## /dev/sda1     ext4      495844     31932    438312       7% /boot
        partion = {}
        diskString = commands.getoutput("df -TlP -x tmpfs | sed 1d").split('\n')
        for item in diskString:
            print item
            item = ' '.join(item.split())
            d = item.split(' ')
            print d
            partion[d[0]] = { 'disktype':d[1],'disktotal':d[2],'diskuse':d[3],\
                            'diskfree':d[4],'diskprecent':d[5],'diskmount':d[6]}
 
        return partion
 
if __name__ == '__main__':
    R = ServerStatus()
    SystemStatus = R.R()
    #sysStat,diskStat,memStat = SystemStatus.R()
    print SystemStatus  
    mongod(SystemStatus)